package cn.edu.sjtu.se.dslab.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.sjtu.se.dslab.auth.CommonVariableModel;
import cn.edu.sjtu.se.dslab.auth.User;
import cn.edu.sjtu.se.dslab.project.PGroup;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.project.UserProject;
import cn.edu.sjtu.se.dslab.service.IPGroupService;
import cn.edu.sjtu.se.dslab.service.IProjectService;
import cn.edu.sjtu.se.dslab.service.IUserProjectService;
import cn.edu.sjtu.se.dslab.service.IUserService;

@Controller
@RequestMapping(value = "/userProject")
public class UserProjectController {
	private static final Logger logger = Logger
			.getLogger(ProjectController.class);

	@Resource(name = "projectService")
	IProjectService projectService;

	@Resource(name = "pgroupService")
	IPGroupService pgroupService;

	@Resource(name = "userService")
	IUserService userService;

	@Resource(name = "userProjectService")
	IUserProjectService userProjectService;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addProject(@RequestParam Long projectId,
			@RequestParam Long userId, @RequestParam Boolean admin) {
		UserProject userProject = new UserProject();
		Project project = projectService.getProjectById(projectId);
		User user = userService.getUserById(userId);
		if (project != null && user != null) {
			userProject.setProject(project);
			userProject.setUser(user);
			userProject.setAdmin(admin);
			userProjectService.addUserProject(userProject);
		} else {
			System.out.println("no user logined");
		}
		return "redirect:/project/list/" + projectId + "/user";
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public String deleteUserProject(@RequestParam Long[] userProjectId, @RequestParam Long projectId){
		userProjectService.deleteUserProjectByIds(userProjectId);
		return "redirect:/project/list/" + projectId + "/user";
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView getProjectList() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("manageProject");
		User user = getUser();
		if (user != null) {
			List<UserProject> userProjects = userProjectService
					.getListByUser(user);
			mav.addObject("userProjects", userProjects.iterator());
		}
		return mav;
	}

	@RequestMapping(value = "/addProjects", method = RequestMethod.GET)
	public ModelAndView addProject() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("addProjects");
		List<PGroup> pgroups = pgroupService.getPGroupList();
		mav.addObject("pgroups", pgroups.iterator());
		return mav;
	}

	@RequestMapping(value = "/addUserProjects", method = RequestMethod.GET)
	public ModelAndView toAddUserProjects() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("addUserProjects");
		return mav;
	}

	@RequestMapping(value = "/list/json", method = RequestMethod.GET)
	@ResponseBody
	public List<Project> getProjectJsonList() {
		User user = getUser();
		List<Project> projects = null;
		if (null != user) {
			projects = userProjectService.getProjectListByUser(user);
			logger.info(projects.size());
			for (Project p : projects) {
				p.setModel(null);
				p.setDesignPatternComponents(null);
				p.setOwner(null);
				p.setPgroup(null);
				logger.info(p.getName());
			}
		}
		return projects;
	}

	private User getUser() {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null && subject.getPrincipals() != null) {
			CommonVariableModel model = (CommonVariableModel) subject
					.getPrincipals().getPrimaryPrincipal();
			return model.getUser();
		}
		return null;
	}

}
